//
//  AppDelegate.m
//  YunGou
//
//  Created by macbook on 16/5/16.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "AppDelegate.h"
#import "RootTabbarViewController.h"
#import "AppDelegate+Umeng.h"
#import "AppDelegate+JPush.h"
#import "UMSocial.h"
#import <Pingpp.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootTabbarViewController *tabbar = [[RootTabbarViewController alloc] init];
    self.window.rootViewController = tabbar;
    
    if (IsLogin) {
        if ([UserDefaults boolForKey:@"ThirdLogin"]) {
            [self loginWithIsThird:YES];
        }else{
           [self loginWithIsThird:NO];
        }
    }
        
    //配置友盟信息
    [self configAccount];
    //极光推送
    [self JpushApplication:application didFinishLaunchingWithOptions:launchOptions appkey:@"00768b050b1c9c002b285081"];
    return YES;
}

- (void)loginWithIsThird:(BOOL)isThird{
    if (isThird) {
        [NetWorkTool thirdLoginWithThirdPartyUid:[UserDefaults objectForKey:@"uid"] success:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                UserModel *model  = [UserModel sharedInstance];
                [model clear];
                UserModel *temp = [UserModel mj_objectWithKeyValues:object[@"data"][@"register_info"]];
                [model mj_setKeyValues:[temp mj_keyValues]];
            }
        } failure:^(NSError *error) {
            
        }];
    }else{
        [NetWorkTool loginWithPhone:[UserDefaults objectForKey:@"phone"] password:[UserDefaults objectForKey:@"password"] success:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                UserModel *model  = [UserModel sharedInstance];
                [model clear];
                UserModel *temp = [UserModel mj_objectWithKeyValues:object[@"data"][@"register_info"]];
                [model mj_setKeyValues:[temp mj_keyValues]];
            }
        } failure:^(NSError *error) {
            
        }]; 
    }
}

#pragma -mark ping++ 接收并处理交易结果
//iOS9 以及以上
- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary *)options {
    BOOL canHandleURL = [Pingpp handleOpenURL:url withCompletion:nil];
    if (!canHandleURL) {
        [UMSocialSnsService handleOpenURL:url];
    }
    return canHandleURL;
}

//iOS8 以及以下
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
        [Pingpp handleOpenURL:url withCompletion:nil];
    }
    return result;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [UMSocialSnsService  applicationDidBecomeActive];
}

@end
